package com.genymobile.scrcpy.five;

import android.os.SystemClock;
import android.view.InputEvent;
import android.view.KeyEvent;
import android.view.MotionEvent;

public class InputEventToSendEvent {
    /**
     * 将 InputEvent 转换为 sendevent 命令行
     *
     * @param inputEvent 输入事件对象 (KeyEvent 或 MotionEvent)
     * @param devicePath 输入设备路径 (如 "/dev/input/event1")
     * @return sendevent 命令字符串
     */
    public static String convertToSendEventCommand(InputEvent inputEvent, String devicePath) {
        if (inputEvent instanceof KeyEvent) {
            return convertKeyEventToSendEvent((KeyEvent) inputEvent, devicePath);
        } else if (inputEvent instanceof MotionEvent) {
            return convertMotionEventToSendEvent((MotionEvent) inputEvent, devicePath);
        } else {
            throw new IllegalArgumentException("Unsupported InputEvent type: " + inputEvent.getClass());
        }
    }

    private static String convertKeyEventToSendEvent(KeyEvent keyEvent, String devicePath) {
        StringBuilder command = new StringBuilder();

        // Key press (down) event
        if (keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
            command.append(String.format("sendevent %s 1 %d 1\n", devicePath, keyEvent.getKeyCode())); // Key down
            command.append(String.format("sendevent %s 0 0 0\n", devicePath)); // Sync
        }

        // Key release (up) event
        if (keyEvent.getAction() == KeyEvent.ACTION_UP) {
            command.append(String.format("sendevent %s 1 %d 0\n", devicePath, keyEvent.getKeyCode())); // Key up
            command.append(String.format("sendevent %s 0 0 0\n", devicePath)); // Sync
        }

        return command.toString();
    }

    private static String convertMotionEventToSendEvent(MotionEvent motionEvent, String devicePath) {
        StringBuilder command = new StringBuilder();

        int pointerCount = motionEvent.getPointerCount();
        for (int i = 0; i < pointerCount; i++) {
            int x = (int) motionEvent.getX(i);
            int y = (int) motionEvent.getY(i);

            // Touch press or move event
            if (motionEvent.getActionMasked() == MotionEvent.ACTION_DOWN) {
                command.append(String.format("sendevent %s 1 330 1\n", devicePath));
                command.append(String.format("sendevent %s 3 57 0\n", devicePath));
                command.append(String.format("sendevent %s 3 53 %d\n", devicePath, x)); // ABS_MT_POSITION_X
                command.append(String.format("sendevent %s 3 54 %d\n", devicePath, y)); // ABS_MT_POSITION_Y
                command.append(String.format("sendevent %s 0 0 0\n", devicePath)); // Sync
            }

            if (motionEvent.getActionMasked() == MotionEvent.ACTION_MOVE) {
                command.append(String.format("sendevent %s 3 53 %d\n", devicePath, x)); // ABS_MT_POSITION_X
                command.append(String.format("sendevent %s 3 54 %d\n", devicePath, y)); // ABS_MT_POSITION_Y
                command.append(String.format("sendevent %s 0 0 0\n", devicePath)); // Sync
            }

            // Touch release event
            if (motionEvent.getActionMasked() == MotionEvent.ACTION_UP ||
                    motionEvent.getActionMasked() == MotionEvent.ACTION_POINTER_UP) {
                command.append(String.format("sendevent %s 3 57 -1\n", devicePath)); // End slot
                command.append(String.format("sendevent %s 1 330 0\n", devicePath));
                command.append(String.format("sendevent %s 0 0 0\n", devicePath)); // Sync
            }

            break;
        }

        return command.toString();
    }
}
